package com.sc.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sc.model.User;
import com.sc.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import  org.springframework.web.servlet.mvc.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@org.springframework.stereotype.Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/showUser.do")
    public void selectUser(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws Exception {
       // request.setCharacterEncoding("UTF-8");
       // response.setCharacterEncoding("UTF-8");
        int  userId = Integer.parseInt(request.getParameter("id").trim());
        User user = this.userService.selectUser(userId);
        ObjectMapper mapper = new ObjectMapper();
        response.getWriter().write(mapper.writeValueAsString(user));
        response.getWriter().close();

    }
    @RequestMapping("/add.do")
    public void add(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        userService.add(username,password);
        //return new ModelAndView("welcome");
    }


}
